package com.chat.chatting.controller;

import com.chat.chatting.dto.DataJson;
import com.chat.chatting.dto.UserAddFormDto;
import com.chat.chatting.entity.UserEntity;
import com.chat.chatting.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 用户表控制层
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 通过用户ID查询单条数据
     *
     * @param userID 用户ID
     * @return 单条数据
     */
    @GetMapping("queryByUserID")
    public UserEntity queryByUserID(Integer userID) {
        return this.userService.queryByUserID(userID);
    }

    /**
     * 创建新用户
     * @return 用户ID
     */
    @GetMapping("create")
    public Integer create(@Param("password") String password,
                          @Param("nickName") String nickName,
                          @Param("phoneNumber") String phoneNumber,
                          @Param("email") String email,
                          @Param("personalizedSignature") String personalizedSignature) {
        UserAddFormDto dto = new UserAddFormDto();
        dto.setPassword(password);
        dto.setNickName(nickName);
        dto.setPhoneNumber(phoneNumber);
        dto.setEmail(email);
        dto.setPersonalizedSignature(personalizedSignature);
        return userService.create(dto);
    }

    /**
     * 登录
     * @param userID   用户ID
     * @param password 密码
     * @return
     */
    @RequestMapping("login")
    public DataJson login(@RequestParam("userID") Integer userID, @RequestParam("password") String password) {
        return userService.login(userID, password);
    }

    /**
     * 上传头像
     * @param file   头像
     * @param userID 用户ID
     * @return 上传结果
     */
    @PostMapping("uploadImage")
    public DataJson uploadImage(@RequestParam("file") MultipartFile file, @RequestParam("userID") Integer userID) {
        return userService.uploadImage(file, userID);
    }

    /**
     * 获取头像
     * @param userID 用户ID
     * @return 头像名称
     */
    @RequestMapping("getImage")
    public String getImage(@RequestParam("userID") Integer userID) {
        return userService.getImage(userID);
    }

    /**
     * 修改昵称
     * @param userID 用户ID
     * @param nickName 要修改的昵称
     * @return
     */
    @RequestMapping("updateNickName")
    public DataJson updateNickName(@RequestParam("userID") Integer userID, @RequestParam("nickName")String nickName) {
        return userService.updateNickName(userID,nickName);
    }

}